// pages/testList/testList.js
var app=getApp()
var util=require('../../utils/util')
Page({
  data: {
    index:0,
    totalScore:100, 
    checked:false,
    check:[],
    chooseValue:[],
    wrongList:[],
    questionList:[]
  },
  onLoad:function()
  {
    let that=this;
    wx.cloud.database().collection("testList").get({
      success(res){
        console.log(res)
        that.setData({
          questionList:res.data,
        })
      },
      fail(res){
        console.log(res)
      }
    })
    
  },
  /*
  * 单选事件
  */
 radioChange: function (e) {
  console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    this.data.chooseValue[this.data.index] = e.detail.value;
    console.log(this.data.chooseValue);
  },
  /*
  * 多选事件
  */
  checkboxChange: function (e) {
  var that=this
  console.log('checkbox发生change事件，携带value值为：', e.detail.value)
  // var check=that.data.check
  // check=check.push(e.currentTarget.dataset.value)
  // console.log(check)
  this.data.chooseValue[this.data.index] = e.detail.value.sort();
  // that.data.checked=checked
  },
  nextSubmit()
  {
    if (this.data.chooseValue[this.data.index] == undefined || this.data.chooseValue[this.data.index].length == 0) {
      wx.showToast({
        title: '请选择至少一个答案!',
        icon: 'none',
        duration: 2000,
        success: function () {
          return;
        }
      })
      return;
    }
    this.chooseError();
    if (this.data.index < this.data.questionList.length - 1) {
      // 渲染下一题
      this.setData({
        index: this.data.index + 1
      })
    }
    else
    {
      let time=util.formatTime2(new Date());
      let wrongList = JSON.stringify(this.data.wrongList);
      let chooseValue = JSON.stringify(this.data.chooseValue);
      let questionList = JSON.stringify(this.data.questionList);
        wx.redirectTo({
        url: '../results/results?totalScore=' + this.data.totalScore + '&wrongList=' + wrongList + '&chooseValue=' + chooseValue+ '&questionList=' + questionList + '&time=' + time 
        })
    }
  },
  chooseError: function () {
    var that=this
    var trueValue = this.data.questionList[this.data.index]['true'];
    var chooseVal = this.data.chooseValue[this.data.index];
    console.log('选择了' + chooseVal + '答案是' + trueValue);
    if (chooseVal != trueValue) {
      this.data.wrongList.push(this.data.index);
      this.setData({
        totalScore: (this.data.totalScore - 100/that.data.questionList.length).toFixed(0)  // 扣分操作
      })
    }
  }
})